package com.jinlia.show.common.core.utils;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Objects;

public class CacheUtils {
    private static final Logger logger = LoggerFactory.getLogger(CacheUtils.class);
    private static JdbcTemplate jdbcTemplate;

    public static void newInstance(JdbcTemplate jdbcTemplateIns) {
        if (jdbcTemplate == null) {
            jdbcTemplate = jdbcTemplateIns;
        }
    }

    public static String fromTable(String tableField, String tableName, Object id) {
        if (Objects.isNull(id)) {
            return null;
        }
        String sql = String.format("select %s from jinlialiu.%s where id = %s", tableField, tableName, id);
        String dataValue = null;
        try {
            dataValue = jdbcTemplate.queryForObject(sql, String.class);
        } catch (Exception exception) {
            logger.error("CacheUtils 自动注入失败" + sql, exception);
        }

        return dataValue;
    }
}
